package cn.neu.leon.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.batik.apps.rasterizer.DestinationType;
import org.apache.batik.apps.rasterizer.SVGConverter;
import org.apache.batik.apps.rasterizer.SVGConverterException;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

public class MyConverter extends SVGConverter{
	
	/**
	 * 转换方法
	 * @param sources SVG文件路径
	 * @param destination 目标文件路径
	 * @param type 转换类型，有 image/png | image/jpeg | application/pdf |　image/svg+xml　可选
	 * @param width 导出图片宽度
	 * @return 目标文件名
	 * @throws SVGConverterException
	 */
	public  String conver(String sources,String destination,String type,float width) throws SVGConverterException {
		
		SVGConverter converter = new MyConverter();
		
		// 设置高度，默认是400
		converter.setHeight(400);
		
		// 设置宽度，传入的值
		converter.setWidth(width);
		
		// 设置svg源文件路径，是一个数组，支持多个文件同时转换
		String[] src = {sources};
		converter.setSources(src);
		

		// 设置图片质量
		converter.setQuality(MAXIMUM_QUALITY);
		
		// 记录文件后缀
		String ext = "";
		
		// 更具传入的类型设置导出类型和文件后缀
		if(type.equals("image/png")) {
			converter.setDestinationType(DestinationType.PNG);
			ext = "png";
		} else if(type.equals("image/jpeg")) {
			converter.setDestinationType(DestinationType.JPEG);
			ext = "jpg";
		} else if(type.equals("application/pdf")) {
			converter.setDestinationType(DestinationType.PDF);
			ext = "pdf";
		} else if(type.equals("save/png")){
			File file = new File(destination+"\\chart.png");
			FileOutputStream outputStream =null;
			ext = "png";
			try {
				file.createNewFile();
				outputStream = new FileOutputStream(file);
				try {
					convertToPng(sources,outputStream);
				} catch (TranscoderException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		else {
			return null;
		}

		
		
		// 设置目标文件路径
		converter.setDst(new File(destination+"\\chart."+ext));
		
		// 执行导出
		converter.execute();
		return "chart."+ext;
	}

	private void convertToPng(String sources, FileOutputStream outputStream) throws TranscoderException, IOException {
		 try {
	            byte[] bytes = sources.getBytes("utf-8");
	            PNGTranscoder t = new PNGTranscoder();
	            TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
	            TranscoderOutput output = new TranscoderOutput(outputStream);
	            t.transcode(input, output);
	            outputStream.flush();
	        } finally {
	            if (outputStream != null) {
	                try {
	                    outputStream.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
		
	}
}
